<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="shopName">
      <el-input v-model="dataForm.shopName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="价格状态" prop="priceState">
      <el-input v-model="dataForm.priceState" placeholder="价格状态"></el-input>
    </el-form-item>
    <el-form-item label="运费状态" prop="freightState">
      <el-input v-model="dataForm.freightState" placeholder="运费状态"></el-input>
    </el-form-item>
    <el-form-item label="总库存" prop="invertore">
      <el-input v-model="dataForm.invertore" placeholder="总库存"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="shopPrice">
      <el-input v-model="dataForm.shopPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="prc">
      <el-input v-model="dataForm.prc" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品类型" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="商品类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shopName: '',
          priceState: '',
          freightState: '',
          invertore: '',
          shopPrice: '',
          prc: '',
          typeId: ''
        },
        dataRule: {
          shopName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          priceState: [
            { required: true, message: '价格状态不能为空', trigger: 'blur' }
          ],
          freightState: [
            { required: true, message: '运费状态不能为空', trigger: 'blur' }
          ],
          invertore: [
            { required: true, message: '总库存不能为空', trigger: 'blur' }
          ],
          shopPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          prc: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '商品类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shop/shop/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopName = data.shop.shopName
                this.dataForm.priceState = data.shop.priceState
                this.dataForm.freightState = data.shop.freightState
                this.dataForm.invertore = data.shop.invertore
                this.dataForm.shopPrice = data.shop.shopPrice
                this.dataForm.prc = data.shop.prc
                this.dataForm.typeId = data.shop.typeId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/shop/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopName': this.dataForm.shopName,
                'priceState': this.dataForm.priceState,
                'freightState': this.dataForm.freightState,
                'invertore': this.dataForm.invertore,
                'shopPrice': this.dataForm.shopPrice,
                'prc': this.dataForm.prc,
                'typeId': this.dataForm.typeId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
